﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThirdClassAssignment{
    public partial class MainWindow : Form{
        private string _path = "";
        private string _text = "";
        private Dictionary<string, int> words;

        private void WindowUpdate(){
            ContentArea.Text = _text;

            var count = 0;
            foreach (var key in words.Keys) {
                count += words[key];
            }
            


            WordCount.Text = "单词数: " + count;
            LineCount.Text = "行数: " + _text.Split('\n').Length;
            WordsCount.Text = "";
            foreach (var word in words.Keys) {
                WordsCount.Text += word + "  " + words[word] + Environment.NewLine;
            }
        }
        
        private void ChoseFile_Click(object sender,EventArgs e){
            var dialog = new OpenFileDialog();
            dialog.Title = "请选择文件";
            dialog.Filter = "CS文件(*.cs*)|*.cs*";
            if (dialog.ShowDialog() == DialogResult.OK) {
                _path = dialog.FileName;
                tips.Text = _path;
                _text = FileParse.Read(_path);
                words = FileParse.CountWords(_text);
                WindowUpdate();
            }
        }

        private void Delete_Click(object sender,EventArgs e){
            _text = FileParse.LineDelete(_text);
            WindowUpdate();
        }
        public MainWindow(){
            InitializeComponent();
            ChoseFile.Click += ChoseFile_Click;
            Delete.Click += Delete_Click;
        }
    }
}